package com.ice.work.study.design.pattern.memento;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Description 备忘录模式
 * @Author DELL
 * @Create 2019-11-20
 * @Since 1.0.0
 */
public class MementoPattern {
    public static void main(String[] args) {
        Originator or = new Originator();
        Caretaker cr = new Caretaker();
        or.setState("S0");
        System.out.println("初始状态:" + or.getState());
        //保存状态
        cr.setMemento(or.createMemento());
        or.setState("S1");
        System.out.println("新的状态:" + or.getState());
        //恢复状态
        or.restoreMemento(cr.getMemento());
        System.out.println("恢复状态:" + or.getState());
    }
}

/**
 * 备忘录
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
class Memento {
    private String state;

}

/**
 * 发起人
 */
@Data
class Originator {
    private String state;

    public Memento createMemento() {
        return new Memento(state);
    }

    public void restoreMemento(Memento m) {
        this.setState(m.getState());
    }
}

/**
 * 管理者
 */
@Data
class Caretaker {
    private Memento memento;
}
